package com.kun.valiant.web.controller.valiant.clue;

import com.kun.valiant.activity.service.IActivityService;
import com.kun.valiant.channel.service.IChannelService;
import com.kun.valiant.clue.domain.Clue;
import com.kun.valiant.clue.domain.vo.ClueDetails;
import com.kun.valiant.clue.domain.vo.ClueSummary;
import com.kun.valiant.clue.domain.vo.ClueVO;
import com.kun.valiant.clue.service.IClueService;
import com.kun.valiant.common.annotation.Log;
import com.kun.valiant.common.core.controller.BaseController;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.domain.entity.SysUser;
import com.kun.valiant.common.core.page.TableDataInfo;
import com.kun.valiant.common.enums.BusinessType;
import com.kun.valiant.common.validator.CreateGroup;
import com.kun.valiant.common.validator.EditGroup;
import com.kun.valiant.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 线索 前端控制器
 * @author fangkun
 * @since 2023-06-08
 */
@RestController
@RequestMapping("/valiant/clue")
public class ClueController extends BaseController {

    @Autowired
    IClueService clueService;

    @Autowired
    IChannelService channelService;

    @Autowired
    IActivityService activityService;

    @Autowired
    ISysUserService userService;

    /**
     * 添加线索
     * @param clue
     * @return
     */
    @PreAuthorize("hasPermission('valiant:clue:create')")
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addClue(@Validated(CreateGroup.class) @RequestBody Clue clue) {
        return clueService.addClue(clue);
    }

    /**
     * 添加时下拉选择渠道
     * @return
     */
    @PreAuthorize("hasPermission('valiant:clue:create')")
    @GetMapping("/channels")
    public AjaxResult getAllChannels(){
        return  AjaxResult.success(channelService.list());
    }

    /**
     * 添加时下拉选择活动
     * @return
     */
    @PreAuthorize("hasPermission('valiant:clue:create')")
    @GetMapping("/activity/{channelId}")
    public AjaxResult getActivityByChannelId(@PathVariable Long channelId){
        return  AjaxResult.success(activityService.getActivityByChannelId(channelId));
    }

    /**
     * 查询线索列表
     * @return
     */
    @PreAuthorize("hasPermission('valiant:clue:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClueVO clueVO){
        startPage();
        List<ClueSummary> clue = clueService.selectAllList(clueVO);
        return getDataTable(clue);
    }

    /**
     * 通过部门ID查询用户
     * @param deptId
     * @return
     */
    @GetMapping("/users/{deptId}")
    @PreAuthorize("hasPermission('valiant:clue:assignment')")
    public AjaxResult getUsersByDeptId(@PathVariable Long deptId){
        List<SysUser> list = userService.getUsersByDeptId(deptId);
        return AjaxResult.success(list);
    }

    /**
     * 通过线索ID获取线索详细信息
     * @param clueId
     * @return
     */
    @GetMapping("/{clueId}")
    @PreAuthorize("hasAnyPermissions('valiant:clue:view','valiant:clue:follow')")
    public AjaxResult getClueDetailsByClueId(@PathVariable Long clueId){
        return clueService.getClueDetailsByClueId(clueId);
    }

    /**
     * 跟进线索：更新线索状态，增加跟进记录
     * @param clueDetails
     * @return
     */
    @PostMapping("/follow")
    @PreAuthorize("hasPermission('valiant:clue:follow')")
    public AjaxResult clueFollow(@RequestBody ClueDetails clueDetails){
        return clueService.clueFollow(clueDetails);
    }

    /**
     * 转换无效线索（三次）
     * @param clueDetails
     * @return
     */
    @PostMapping("/invalid")
    @PreAuthorize("hasPermission('valiant:clue:follow')")
    public AjaxResult invalidClueFollow(@RequestBody ClueDetails clueDetails){
        return clueService.invalidClueFollow(clueDetails);
    }

    /**
     * 根据id查询（修改回写）
     * @param clueId
     * @return
     */
    @GetMapping("/summary/{clueId}")
    @PreAuthorize("hasPermission('valiant:clue:edit')")
    public AjaxResult getClueSummaryById(@PathVariable Long clueId){
        return clueService.getClueSummaryById(clueId);
    }

    /**
     * 更新线索
     * @param clue
     * @return
     */
    @PutMapping
    @PreAuthorize("hasPermission('valiant:clue:edit')")
    public AjaxResult updateClue(@Validated(EditGroup.class) @RequestBody Clue clue){
        return clueService.updateClue(clue);
    }

    /**
     * 删除线索
     * @param clueIds
     * @return
     */
    @DeleteMapping("/{clueIds}")
    @PreAuthorize("hasPermission('valiant:clue:remove')")
    public AjaxResult deleteClueById(@PathVariable Long[] clueIds){
        return clueService.deleteClueById(clueIds);
    }

    /**
     * 线索转商机
     * @param clueId
     * @return
     */
    @PostMapping("/to_business/{clueId}")
    @PostAuthorize("hasPermission('valiant:clue:follow')")
    public AjaxResult clue2Business(@PathVariable Long clueId){
        return clueService.clue2Business(clueId);
    }

}
